﻿// <copyright file="AutoUpdate.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-11-07</date>
// <summary>AutoUpdate class definition</summary>

namespace Contoso.WebAnalytics.Design.Editors
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Net;
    using System.Reflection;

    /// <summary>
    /// AutoUpdate for Contos Analytics
    /// </summary>
    public class AutoUpdate : INotifyPropertyChanged
    {
        #region Fields
        /// <summary>
        /// is an update available
        /// </summary>
        private bool isUpdateAvailable;
        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the AutoUpdate class
        /// </summary>
        public AutoUpdate()
        {
            var assembly = GetType().Assembly;

            if (Attribute.IsDefined(assembly, typeof(AssemblyVersionAttribute)))
            {
                var attribute = Attribute.GetCustomAttribute(assembly, typeof(AssemblyVersionAttribute)) as AssemblyVersionAttribute;

                using (var client = new WebClient())
                {
                    var uri = string.Format(
                        CultureInfo.InvariantCulture,
                        "http://www.contoso.com/analtyics/IsUpdateAvailable?current={0}",
                        attribute.Version);

                    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.OnDownloadStringCompleted);

                    client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));
                }
            }
        }
        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Property changed event handler
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets a value indicating whether an update is available
        /// </summary>
        public bool IsUpdateAvailable
        {
            get
            {
                return this.isUpdateAvailable;
            }

            set
            {
                if (this.isUpdateAvailable != value)
                {
                    this.isUpdateAvailable = value;

                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("IsUpdateAvailable"));
                    }
                }
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Download the update
        /// </summary>
        public static void DownloadUpdate()
        {
        }
        #endregion

        #region Implementation
        /// <summary>
        /// Download string complete event handler
        /// </summary>
        /// <param name="sender">the webclient</param>
        /// <param name="e">the download string complete event handler</param>
        private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                System.Diagnostics.Debug.WriteLine(e.Error.Message);

                return;
            }

            bool updateAvailable;

            if (bool.TryParse(e.Result, out updateAvailable))
            {
                this.IsUpdateAvailable = updateAvailable;
            }
        }
        #endregion
    }
}
